//
//  UIScrollView+IGHeaderFooter.m
//  jingdongfang
//
//  Created by BOE on 2017/8/30.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "UIScrollView+IGHeaderFooter.h"
#import <MJRefresh.h>

@implementation UIScrollView (IGHeaderFooter)

- (void)ig_setHeaderWithRefreshingBlock:(void (^)())refreshingBlock
{
    if (self.mj_header) {
        return;
    }
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        refreshingBlock();
    }];
    [header setImages:[self headerIdleImages] forState:MJRefreshStateIdle];
    [header setImages:[self headerPullingImages] duration:1.f forState:MJRefreshStatePulling];
    [header setImages:[self headerRefreshingImages] duration:1.f forState:MJRefreshStateRefreshing];
    header.stateLabel.hidden = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.mj_header = header;
}

- (void)ig_beginHeaderRefreshing
{
    if ([self ig_isHeaderRefreshing]) {
        return;
    }
    [self.mj_header beginRefreshing];
}

- (void)ig_endHeaderRefreshing
{
    if (!self.mj_header) {
        return;
    }
    [self.mj_header endRefreshing];
}

- (BOOL)ig_isHeaderRefreshing
{
    if (!self.mj_header) {
        return NO;
    }
    return self.mj_header.isRefreshing;
}

- (void)ig_setFooterWithRefreshingBlock:(void (^)())refreshingBlock
{
//    [self ig_setFooterWithNoMoreTitle:@"没有更多内容了>.<"
//                      refreshingBlock:refreshingBlock];
    [self ig_setFooterWithNoMoreTitle:NSLocalizedString(@"No more content",nil)
                      refreshingBlock:refreshingBlock];
    
}

- (void)ig_setFooterWithNoMoreTitle:(NSString *)noMoreTitle
                    refreshingBlock:(void (^)())refreshingBlock
{
    if (self.mj_footer) {
        return;
    }
    noMoreTitle = [noMoreTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//    noMoreTitle = noMoreTitle.length > 0 ? noMoreTitle : @"没有更多内容了>.<";
    noMoreTitle = noMoreTitle.length > 0 ? noMoreTitle : NSLocalizedString(@"No more content",nil);
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        refreshingBlock();
    }];
    [footer setImages:[self footerRefreshingImages] duration:1.f forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:13];
    footer.stateLabel.textColor = [UIColor colorWithHex:@"5b5b5b"];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:noMoreTitle forState:MJRefreshStateNoMoreData];
    self.mj_footer = footer;
}

- (void)ig_endFooterRefreshing
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer endRefreshing];
}

- (void)ig_endFooterRefreshingWithNoMoreData
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer endRefreshingWithNoMoreData];
}

- (void)ig_resetFooterToIdle
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer resetNoMoreData];
}

- (BOOL)ig_isFooterRefreshing
{
    if (!self.mj_footer) {
        return NO;
    }
    return self.mj_footer.isRefreshing;
}

- (void)ig_setFooterAutomaticallyHidden:(BOOL)automaticallyHidden
{
    if (!self.mj_footer) {
        return;
    }
    self.mj_footer.automaticallyHidden = automaticallyHidden;
}

- (void)ig_removeFooterRefreshing
{
    if (!self.mj_footer) {
        return;
    }
    [self.mj_footer removeFromSuperview];
    self.mj_footer = nil;
}

#pragma mark -

- (NSArray *)headerIdleImages
{
    static NSMutableArray *headerIdleImages;
    if (!headerIdleImages) {
        NSInteger count = 7;
        headerIdleImages = [NSMutableArray arrayWithCapacity:count + 1];
        for (NSInteger index = 0; index < count; index++) {
            [headerIdleImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"header-idle-%@", @(index)]]];
        }
        UIImage *image = [headerIdleImages lastObject];
        [headerIdleImages insertObject:[image copy] atIndex:0]; // 第一张添加默认尺寸图
    }
    return headerIdleImages;
}

- (NSArray *)headerPullingImages
{
    static NSMutableArray *headerPullingImages;
    if (!headerPullingImages) {
        NSInteger count = 15;
        headerPullingImages = [NSMutableArray arrayWithCapacity:count];
        for (NSInteger index = 0; index < count; index++) {
            [headerPullingImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"header-pulling-%@", @(index)]]];
        }
    }
    return headerPullingImages;
}

- (NSArray *)headerRefreshingImages
{
    static NSMutableArray *headerRefreshingImages;
    if (!headerRefreshingImages) {
        NSInteger count = 17;
        headerRefreshingImages = [NSMutableArray arrayWithCapacity:count];
        for (NSInteger index = 0; index < count; index++) {
            [headerRefreshingImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"header-refreshing-%@", @(index)]]];
        }
    }
    return headerRefreshingImages;
}

- (NSArray *)footerRefreshingImages
{
    static NSMutableArray *footerRefreshingImages;
    if (!footerRefreshingImages) {
        NSInteger count = 30;
        footerRefreshingImages = [NSMutableArray arrayWithCapacity:count];
        for (NSInteger index = 0; index < count; index++) {
            [footerRefreshingImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"footer-refreshing-%@", @(index)]]];
        }
    }
    return footerRefreshingImages;
}

@end
